/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-08-30 00:21:30
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-08-30 17:32:55
 * @FilePath: \keep_motion_api\controllers\permission\permission.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { Permission } = require('@/models/permission');


// 查询权限
const getPermission = async (req, res) => {
  try {
    const { name, code, menu_item, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const {rows, total} = await Permission.getPermission(name, code, menu_item, offset, limit);
    res.status(200).send({rows, total});
  } catch (error) {
    res.status(500).json({ message: '查询字典类型失败!', error });
  }
}

// 同步菜单到权限
const syncMenuToPermission = async (_, res) => {
  try {
    await Permission.syncMenuToPermission();
    res.status(200).send({message: '同步菜单到权限成功!'});
  } catch (error) {
    res.status(500).json({ message: '同步菜单到权限失败!', error });
  }
}

// 变更API到权限
const bindPermission = async (req, res) => {
  try {
    const { _id, apiPaths } = req.body;
    await Permission.bindApiToPermission(_id, apiPaths);
    res.status(200).send({message: '变更API到权限成功!'});
  } catch (error) {
    res.status(500).json({ message: '变更API到权限失败!', error });
  }
}



module.exports = {
    getPermission,
    syncMenuToPermission,
    bindPermission
}